package com.edais.service.imp;

import com.edais.domain.User;
import com.edais.domain.UserInfo;
import com.edais.service.PointService;
import com.edais.util.*;
import com.edais.util.weixinAbout.HttpRequestParam;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 积分相关 Created by haoyx on 2016/10/26.
 */
@Service
public class PointServiceImpl implements PointService {

	private static final Logger logger = Logger.getLogger(PointServiceImpl.class);

	private static final SimpleDateFormat orderNoFormat = new SimpleDateFormat("yyyyMMddHHmms");

	@Async
	@Override
	public void registerPoint(User user) {
		logger.info("#### 注册送积分开始userId" + user.getId() + "  ####");
		try {
			if (null == user.getId() || StringUtils.isEmpty(user.getMobile()))
				return;
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", user.getId());
			params.put("mobile", user.getMobile());
			this.pointSend("/pointaccount/registerPoint", params);
		} catch (Exception e) {
			logger.error("注册送积分", e);
		}
		logger.info("#### 注册送积分 结束userId" + user.getId() + "  ####");
	}

	@Async
	@Override
	public void realnamePoint(User user) {
		logger.info("#### 实名认证送积分userId" + user.getId() + " ####");
		try {
			if (null == user.getId() || StringUtils.isEmpty(user.getMobile()))
				return;
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", user.getId());
			params.put("mobile", user.getMobile());
			this.pointSend("/pointaccount/realnamePoint", params);
		} catch (Exception e) {
			logger.error("实名认证送积分userId" + user.getId(), e);
		}
		logger.info("#### 实名认证送积分结束userId" + user.getId() + " ####");
	}

	@Async
	@Override
	public void dingqiPoint(Map<String, Object> params) {
		logger.info("#### 定期购买送积分 开始 ####");
		logger.info("#### params=" + params + " ####");
		try {
			this.pointSend("/pointaccount/dingqiPoint", params);
		} catch (Exception e) {
			logger.error("定期购买送积分异常", e);
		}
		logger.info("#### 定期购买送积分 结束 ####");
	}

	@Async
	@Override
	public void shopPoint(Map<String, Object> params) {
		logger.info("#### 商城购买送积分 开始 ####");
		logger.info("#### params=" + params + " ####");
		try {
			this.pointSend("/pointaccount/shopDingqiPoint", params);
		} catch (Exception e) {
			logger.error("商城购买送积分异常", e);
		}
		logger.info("#### 商城购买送积分 结束 ####");
	}

	@Async
	@Override
	public void kcodePoint(String logInfo, User user, String kcodeno) {
		logger.info(logInfo + "送积分开始");
		try {
			if (null == user.getId() || StringUtils.isEmpty(user.getMobile()) || StringUtils.isEmpty(kcodeno))
				return;
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", user.getId());
			params.put("mobile", user.getMobile());
			params.put("kcodeno", kcodeno);
			this.pointSend("/pointaccount/kcodePoint", params);
		} catch (Exception e) {
			logger.info(logInfo + "送积分异常");
		}
		logger.info(logInfo + "送积分结束");
	}

	@Async
	@Override
	public void luckyDrawPoint(String userId, String phone, int amount) {
		logger.info("####幸运抽奖送积分送积分开始 userId" + userId + "  ####");
		try {
			if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(phone))
				return;
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("userId", userId);
			params.put("mobile", phone);
			params.put("orderno", generatePointOrderNo());
			params.put("amount", amount);
			this.pointSend("/pointaccount/luckyDrawPoint", params);
		} catch (Exception e) {
			logger.error("幸运抽奖送积分异常", e);
		}
		logger.info("#### 幸运抽奖送积分 结束userId" + userId + " ####");
	}

	/**
	 * 积分请求发送
	 * 
	 * @param path
	 *            请求路径
	 * @param params
	 *            请求参数
	 * @return 请求结果
	 */
	private JSONObject pointSend(String path, Map<String, Object> params) {
		logger.info("#### 积分请求发送 开始 ####");
		if (StringUtils.isEmpty(path))
			throw new NullPointerException("path is null");
		logger.info("#### path=" + path + " ####");
		logger.info("#### 参数列表----> " + params.toString());
		String param = CommonUtil.getParam(params);
		try {
			param = DES3Util.encode(param);
		} catch (Exception e) {
			logger.error("积分请求发送，加密失败", e);
		}
		// 调用service接口
		String resultMsg = HttpRequestParam.sendGet(LoginRedirectUtil.interfacePathForShop + path, param);
		try {
			resultMsg = java.net.URLDecoder.decode(DES3Util.decode(resultMsg), "UTF-8");
		} catch (Exception e) {
			logger.error("积分请求发送，解密失败", e);
			e.printStackTrace();
		}
		logger.info("#### 请求结果---->" + resultMsg);
		logger.info("#### 积分请求发送 结束 ####");
		return JSONObject.fromObject(resultMsg);
	}

	@Override
	public JSONObject getPoint(String userId, String phone) throws Exception {
		String logInfo = "根据用户ID调shop系统获得积分信息userId=" + userId;
		logger.info(logInfo);

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", userId);
		params.put("phone", phone);
		params.put("type", 2);
		JSONObject jb = this.pointSend("/point/pointSwitch", params);

		return jb;
	}

	@Override
	public String generatePointOrderNo() {
		return orderNoFormat.format(new Date()) + RandomUtil.radomCode(3);
	}
}
